package com.${companyName}.${projectName}.dao.${model}.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.${companyName}.${projectName}.model.${model}.${objectName};
import com.${companyName}.${projectName}.dao.MybatisTemplate;
import com.${companyName}.${projectName}.dao.${model}.I${objectName}Dao;
import com.${companyName}.${projectName}.tools.pager.PagerModel;
import com.${companyName}.${projectName}.tools.pager.Query;


<#assign primaryKeyTypeJava="String"/> 
<#if columnFieldList??>  
	<#list columnFieldList as item>
		<#if item.column=='${primaryKeys[0]}' && item.jdbcType=='INTEGER'>
			<#assign primaryKeyTypeJava="Integer"/>
		</#if>
	</#list>
</#if>


/**
 * ${objectNameCn}Dao实现
 * @author ${author}
 * @date ${createTime}
 */
@Repository
public class ${objectName}DaoImpl extends MybatisTemplate implements I${objectName}Dao {

	@Override
	public ${objectName} get${objectName}By${primaryKeyMerger}(${primaryKeyTypeJava} ${primaryKeysConvert[0]}) throws Exception {
		return (${objectName})this.selectOne("${objectName}XML.get${objectName}By${primaryKeyMerger}", ${primaryKeysConvert[0]});
	}
	
	@Override
	public List<${objectName}> get${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("${primaryKeysConvert[0]}s", ${primaryKeysConvert[0]}s);
		return this.selectList("${objectName}XML.get${objectName}By${primaryKeyMerger}s", params);
	}
	
	@Override
	public List<${objectName}> get${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s) throws Exception {
		return this.selectList("${objectName}XML.get${objectName}By${primaryKeyMerger}sList", ${primaryKeysConvert[0]}s);
	}

	@Override
	public List<${objectName}> getAll(${objectName} ${fLowerObjectName}) throws Exception {
		return this.selectList("${objectName}XML.getAll", ${fLowerObjectName});
	}

	@Override
	public PagerModel<${objectName}> getPagerModelByQuery(${objectName} ${fLowerObjectName}, Query query)
			throws Exception {
		return this.getPagerModelByQuery(${fLowerObjectName}, query, "${objectName}XML.getPagerModelByQuery");
	}
	
	@Override
	public int getByPageCount(${objectName} ${fLowerObjectName})throws Exception {
		return this.selectOne("${objectName}XML.getByPageCount", ${fLowerObjectName});
	}

	@Override
	public void insert${objectName}(${objectName} ${fLowerObjectName}) throws Exception {
		if (null != ${fLowerObjectName}) {
			<#if primaryKeyTypeJava="String">
			${fLowerObjectName}.set${primaryKeyMerger}(UUIDGenerator.generate());
			</#if>
			${fLowerObjectName}.setCreateTime(new Date());
			${fLowerObjectName}.setUpdateTime(new Date());
			this.insert("${objectName}XML.insert${objectName}", ${fLowerObjectName});
		}
	}
	
	@Override
	public void insert${objectName}Batch(List<${objectName}> ${fLowerObjectName}s) throws Exception {
		if(CollectionUtils.isNotEmpty(${fLowerObjectName}s)){
			for (${objectName} ${fLowerObjectName} : ${fLowerObjectName}s) {
				if (null != ${fLowerObjectName}) {
					<#if primaryKeyTypeJava="String">
					${fLowerObjectName}.set${primaryKeyMerger}(UUIDGenerator.generate());
					</#if>
					${fLowerObjectName}.setCreateTime(new Date());
				}
			}
			this.insert("${objectName}XML.insert${objectName}Batch", ${fLowerObjectName}s);
		}
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}(${primaryKeyTypeJava} ${primaryKeysConvert[0]}) throws Exception {
		this.delete("${objectName}XML.del${objectName}By${primaryKeyMerger}", ${primaryKeysConvert[0]});
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("${primaryKeysConvert[0]}s", ${primaryKeysConvert[0]}s);
		this.delete("${objectName}XML.del${objectName}By${primaryKeyMerger}s", params);
	}
	
	@Override
	public void del${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s) throws Exception {
		this.delete("${objectName}XML.del${objectName}By${primaryKeyMerger}sList", ${primaryKeysConvert[0]}s);
	}
	
	@Override
	public int update${objectName}(${objectName} ${fLowerObjectName}) throws Exception {
		if (null != ${fLowerObjectName}) {
			${fLowerObjectName}.setUpdateTime(new Date());
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(${fLowerObjectName});
			return this.update("${objectName}XML.update${objectName}", ${fLowerObjectName});
		} else {
			return 0;
		}
	}
	
	@Override
	public int update${objectName}By${primaryKeyMerger}s(String ${primaryKeysConvert[0]}s,${objectName} ${fLowerObjectName}) throws Exception {
		${primaryKeysConvert[0]}s = StringTools.converString(${primaryKeysConvert[0]}s);
		if (StringUtils.isNotBlank(${primaryKeysConvert[0]}s) && null != ${fLowerObjectName}) {
			${fLowerObjectName}.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("${primaryKeysConvert[0]}s", ${primaryKeysConvert[0]}s);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(${fLowerObjectName});
			params.put("${fLowerObjectName}", ${fLowerObjectName});
			return this.update("${objectName}XML.update${objectName}By${primaryKeyMerger}s", params);
		} else {
			return 0;
		}
		
	}
	
	@Override
	public int update${objectName}By${primaryKeyMerger}sList(List<${primaryKeyTypeJava}> ${primaryKeysConvert[0]}s,${objectName} ${fLowerObjectName}) throws Exception {
		if (CollectionUtils.isNotEmpty(${primaryKeysConvert[0]}s) && null != ${fLowerObjectName}) {
			${fLowerObjectName}.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("${primaryKeysConvert[0]}s", ${primaryKeysConvert[0]}s);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(${fLowerObjectName});
			params.put("${fLowerObjectName}", ${fLowerObjectName});
			return this.update("${objectName}XML.update${objectName}By${primaryKeyMerger}sList", params);
		} else {
			return 0;
		}
	}
	
	@Override
	public int update${objectName}List(List<${objectName}> ${fLowerObjectName}s) throws Exception {
		if(CollectionUtils.isNotEmpty(${fLowerObjectName}s)){
			for (${objectName} ${fLowerObjectName} : ${fLowerObjectName}s) {
				if (null != ${fLowerObjectName}) {
					${fLowerObjectName}.setUpdateTime(new Date());
					/**清理不需要更新的数据*/
					this.cleanWhenUpdate(${fLowerObjectName});
				}
			}
			return this.update("${objectName}XML.update${objectName}List", ${fLowerObjectName}s);
		} else {
			return 0;
		}
	}
	
	/**
	 * 清理不需要更新的数据
	 * @param orderRebok
	 * @Description:
	 * @author wentaoxiang 2016年6月1日 下午5:19:16
	 */
	private void cleanWhenUpdate(${objectName} ${fLowerObjectName}) {
		${fLowerObjectName}.setCreateTime(null);
		${fLowerObjectName}.setCreator(null);
	}
	
	//------------api------------
}

